package zpump

import (
	"gitee.com/youkelike/ziam/zapp/app"
	"gitee.com/youkelike/ziam/zpump/config"
	"gitee.com/youkelike/ziam/zpump/options"
	"gitee.com/youkelike/ziam/zshutdown"
	"gitee.com/youkelike/zlog"
)

const commandDesc = "Zpump"

func NewApp(basename string) *app.App {
	opts := options.NewOptions()
	application := app.NewApp("zpump server",
		basename,
		app.WithOptions(opts),
		app.WithDescription(commandDesc),
		app.WithDefaultValidArgs(),
		app.WithRunFunc(run(opts)),
	)

	return application
}

func run(opts *options.Options) app.RunFunc {
	return func(basename string) error {
		zlog.Init(opts.Log)
		defer zlog.Sync()

		cfg, err := config.CreateConfigFromOptions(opts)
		if err != nil {
			return err
		}

		// 最简单的优雅关停实现
		stopCh := zshutdown.SetupSignalHandler()

		return Run(cfg, stopCh)
	}
}
